package homework.todolist.service.impl;

import homework.todolist.enums.ResponseEnum;
import homework.todolist.mapper.TodoMapper;
import homework.todolist.pojo.Todo;
import homework.todolist.service.TodoService;
import homework.todolist.utils.ResponseResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class TodoServiceImpl implements TodoService {

    @Resource
    private TodoMapper todoMapper;

    private static final String PICTURE_PATH = "src/main/resources/picture/";

    private static final List ALLOW_IMAGE_TYPE = Arrays.asList("image/jpeg");
    private Process log;

    @Override
    // TODO: 2021/4/7
    public ResponseResult<Todo> createTodo(String content) {
        Todo todo = new Todo();
        //将初始状态设为0 未完成
        todo.setStatus(0);
        //设置上传时间
        todo.setUpdateTime(new Date());
        todo.setContent(content);
        int todo1 = todoMapper.createTodo(todo);
        if(todo1 > 0) {
            return new ResponseResult<Todo>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), todo);
        }else{
            return new ResponseResult<>(ResponseEnum.FAIL.getCode(),ResponseEnum.FAIL.getMsg(),todo );
        }
    }

    @Override
    public ResponseResult<Void> updateContent(Integer id, String content) {
        int i = todoMapper.updateContent(id, content);
        int i1 = todoMapper.updateDate(id, new Date());
        if(i > 0 && i1 > 0) {
            return ResponseResult.ok();
        }else{
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult<Void> updateStatus(Integer id) {
        Todo byId = todoMapper.findById(id);
        //需要加这个判断，不然如果传入的id在数据库中没有的话就会报 java.lang.NullPointerException
        if (byId == null){
            return new ResponseResult(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMsg(), "fail");
        }
        Integer status = byId.getStatus();
        if(status == 1){
            status = 0;
        }else {
            status = 1;
        }
        int i = todoMapper.updateStatus(id, status);
        //这里再加一次判断，防止前面对了而后面出现问题导致报错
        if(i > 0) {
            todoMapper.updateDate(id, new Date());
            return ResponseResult.ok();
        }else{
            return ResponseResult.fail();
        }
    }

    // TODO: 2021/4/7
    @Override
    public ResponseResult<String> addPicture(Integer id, MultipartFile picture) throws FileNotFoundException {

        String extension = "." + FilenameUtils.getExtension(picture.getOriginalFilename());
        String filename = UUID.randomUUID().toString()+extension;
//
        String realPath = ResourceUtils.getURL("classpath:").getPath() + "/picture/";
        String url = realPath + filename+ extension;

        File dir = new File(url);
//        System.out.println(dir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            picture.transferTo(new File(dir,filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = todoMapper.addPicture(id,url);
        if(i > 0) {
            todoMapper.updateDate(id,new Date());
            return ResponseResult.ok(url);
        }else{
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult<List<Todo>> getTodoList() {
        List<Todo> todoList = todoMapper.getTodoList();
        return ResponseResult.ok(todoList);
    }

    @Override
    public ResponseResult<Void> deleteTodo(Integer id) {
        int i = todoMapper.deleteTodo(id);
        if(i > 0) {
            return ResponseResult.ok();
        }else {
            return ResponseResult.fail();
        }
    }
}
